package yanghuitriangle;

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public static List<List<Integer>> generate (int numRows) {
        List<List<Integer>> lists = new ArrayList<>();

        int numCols = 0;
        //第一行直接是1
        List<Integer> head = new ArrayList<>();
        head.add(1);
        lists.add(head);

        for (int i = 1; i < numRows; i++) {
            List<Integer> list = new ArrayList<>();
            list.add(1);   //第一个元素都是1

            List<Integer> preList = lists.get(i - 1);

            for (int j = 1; j < i; j++) {    //因为j<i且j=1 所以get(j)和get(j-1)不会越界
                list.add(preList.get(j) + preList.get(j - 1));
            }

            list.add(1);    //结尾元素也是1
            lists.add(list);
        }

        return lists;
    }
}

class Ts {
    public static void main(String[] args) {
        System.out.println(Solution.generate(5));
    }
}
